/**
 * Created by Administrator on 2015/11/12.
 */
'use strict';
var F = require('../common/function');
var mysql = require('../common/mysql.js');
var C = require('../config/index');
var _o2oServiceApi = require('../common/o2oServiceApi');
var o2oServiceApi = new _o2oServiceApi;

//瓷肌百科页面操作类
exports.getSection = function *(obj_wiki) {
  let obj_result = {};
  if (obj_wiki) {
    obj_result.type = obj_wiki.param.type;
    obj_result.style = obj_wiki.param.style;
    if(obj_result.type !=16){
      obj_result.title = obj_wiki.data.title;
      obj_result.desc = obj_wiki.data.desc;
    }
  }
  return obj_result;
};
exports.addPage = function *(obj_wiki) {
  let pageResult = yield o2oServiceApi.post('wiki', 'add', 1, obj_wiki);
  if (parseInt(pageResult['result']['error']) == 1 || pageResult.status == 400) {
    return F.returnMsg(300, pageResult['message']['msg'], 1);
  } else {
    if(pageResult.status == 200){
      this.setPageCache(pageResult.result.dataValues);
      return F.returnMsg(200, '添加成功', 1, pageResult.result.dataValues);
    }
  }
};

exports.updatePage = function *(obj_wiki) {
  let pageResult = yield o2oServiceApi.post('wiki', 'update', 1, obj_wiki);
  if (parseInt(pageResult['result']['error']) == 1 || pageResult.status == 400) {
    return F.returnMsg(300, pageResult['message']['msg'], 1);
  } else {
    if (pageResult.status == 200) {
      this.setPageCache(pageResult['result']);
    }
    return F.returnMsg(200, '更新成功', 1, pageResult['result']);
  }
};

exports.setPageCache=function (object_page){
  F.redisClient.hmset('wiki:page:type_'+object_page.type+':typeId_'+object_page.idFromType, 'pageName', object_page.pageName, 'content', object_page.content, 'type', object_page.type, 'idFromType', object_page.idFromType, 'status', object_page.status,'id',object_page.id);
  F.redisClient.expire('wiki:page:type_'+object_page.type+':typeId_'+object_page.idFromType, C.session.ttl);

  F.redisClient.hmset('wiki:page:id_' + object_page.id, 'pageName', object_page.pageName, 'content', object_page.content, 'type', object_page.type, 'idFromType', object_page.idFromType, 'status', object_page.status,'id',object_page.id);
  F.redisClient.expire('wiki:page:id_' + object_page.id, C.session.ttl);
};

exports.getPageList = function *(obj_param){
  let pageResult = yield o2oServiceApi.post('wiki', 'list', 1, obj_param);
  if (parseInt(pageResult['result']['error']) == 1 || pageResult.status == 400) {
    return F.returnMsg(300, pageResult['message']['msg'], 1);
  } else {
    if(pageResult['result'].length==0){
      return F.returnMsg(300, '已经没有数据了', 1);
    }
    return F.returnMsg(200, '查找成功', 1, pageResult['result']);
  }
};

exports.getPageByTypeNId=function*(type,idFromType,isFromApp){
  let ret_obj={type:type,idFromType:idFromType};
  let exist = yield F.redisCo.exists('wiki:page:type_'+type+':typeId_'+idFromType);
  if(exist==1){
    let obj=yield F.redisCo.hmget('wiki:page:type_'+type+':typeId_'+idFromType,['pageName', 'content','status','id']);
    ret_obj.pageName=obj[0];
    ret_obj.data_json=JSON.parse(obj[1]);
    ret_obj.status=obj[2];
    ret_obj.id=obj[3];
  }else{
    let wiki_data = yield o2oServiceApi.post('wiki', 'detail', 1, {act:2,type:type,idFromType:idFromType});
    if (parseInt(wiki_data['result']['error']) == 1 || wiki_data.status == 400){
      return F.returnMsg(300,'没有对应瓷肌百科页面',1);
    }else{
      this.setPageCache(wiki_data['result']);
      wiki_data=wiki_data['result'];
      ret_obj.pageName=wiki_data.pageName;
      ret_obj.data_json=JSON.parse(wiki_data.content);
      ret_obj.status=wiki_data.status;
      ret_obj.id=wiki_data.id;
    }
  }
  if(isFromApp && isFromApp==1){
    let wiki_obj_array=[];
    for(let item of ret_obj.data_json.items){
      wiki_obj_array.push(yield this.getSection(item));
    }
    delete ret_obj.data_json;
    ret_obj.items=wiki_obj_array;
  }
  return ret_obj;

};
exports.getPageById = function *(id){
  let ret_obj={id:id};
  let exist = yield F.redisCo.exists('wiki:page:id_' + id);
  if(exist==1){
    let obj= yield F.redisCo.hmget('wiki:page:id_' + id, ['pageName', 'content','type','idFromType','status']);
    ret_obj.pageName=obj[0];
    ret_obj.data_json=JSON.parse(obj[1]);
    ret_obj.type=obj[2];
    ret_obj.idFromType=obj[3];
    ret_obj.status=obj[4];
  } else {
    let wiki_data = yield o2oServiceApi.post('wiki', 'detail', 1, {act: 1, id: id});
    if (parseInt(wiki_data['result']['error']) == 1 || wiki_data.status == 400) {
      return F.returnMsg(400, '没有对应瓷肌百科页面', 1);
    } else {
      this.setPageCache(wiki_data['result']);
      wiki_data = wiki_data['result'];
      ret_obj.pageName = wiki_data.pageName;
      ret_obj.data_json = JSON.parse(wiki_data.content);
      ret_obj.type = wiki_data.type;
      ret_obj.idFromType = wiki_data.idFromType;
      ret_obj.status = wiki_data.status;
    }
  }
  return ret_obj;
};

exports.getPageByIdForApp = function *(id){
  let ret_obj=yield this.getPageById(id);
  let section_items=[];
  for(let i in ret_obj.data_json.items){
    let item=ret_obj.data_json.items[i];
    section_items.push(yield this.getSection(item));
  }
  ret_obj.items=section_items;
  delete ret_obj.data_json;
  return F.returnMsg(200,'成功',3,ret_obj);
};

exports.getAllPage=function *(){
  let wiki_pages=yield mysql.query('SELECT id,pageName from mh_wiki');
  if (wiki_pages && wiki_pages.length > 0) {
    return F.returnMsg(200,'成功',3,wiki_pages);
  }else{
    return F.returnMsg(300,'已经没有数据了',1);
  }
};
